<?php

namespace App\Http\Controllers\Home;

use App\Http\Servers\Home\BaseServer;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class ChunkController extends CommonController
{
    //模块展示页面
    public function show(Request $request, BaseServer $baseServer)
    {
        // 根据请求过来的chunk数据，及状态查找数据，使用get获取结果集，使用toArray将其转换成数组形式
        $tmpChunk = \App\Chunk::where('path', $request->chunk)
            ->where('status', 1)
            ->get()
            ->toArray();
        // 若$tempChunk中有数据，将其传给$data['chunk'],若没有，则抛出abort异常
        $tmpChunk ? $data['chunk'] = $tmpChunk[0] : abort(404);
        // 获取config中area里的数据,若有，则拼接数据并赋值给$data['title']
        if (config('area')) {
            $data['title'] = $data['chunk']['name'] . '_' . config('set.web_name') . '_' . config('range')[config('area')] . '分站';
        } else {
            $data['title'] = $data['chunk']['name'] . '_' . config('set.web_name');
        }

        $data['keywords'] = config('set.web_keywords');
        // 使用地区关键词处理函数处理config中的keywords。 $baseServer为形参，但可直接调用BaseServer.php中的方法
        $baseServer->keywordDowork($request, $data['keywords']);

        $data['description'] = !empty($data['chunk']['simpledescribe']) ? $data['chunk']['simpledescribe'] : config('set.web_description');

        $data['frames'] = $baseServer->frame([
            ['url' => route('show', $request->chunk), 'name' => $data['chunk']['name'] . '展示'],
        ]);

        return view('home.pc.chunk.' . $data['chunk']['theme'], $data);
    }
}
